<template>
  <div class="table-box">
    <ProTable ref="proTable" title="通用类型管理列表" row-key="id" :indent="20" :columns="columns" :request-api="getCustomList">
      <template #tableHeader>
        <el-button v-auth="'add'" type="primary" :icon="CirclePlus" @click="openDialog('新增', initData)"> 新增 </el-button>
      </template>
      <template #operation="scope">
        <el-button v-auth="'view'" type="primary" link :icon="EditPen" @click="openDialog('查看', scope.row)"> 查看 </el-button>
        <el-button v-auth="'edit'" type="primary" link :icon="EditPen" @click="openDialog('编辑', scope.row)"> 编辑 </el-button>
        <el-button v-auth="'delete'" type="primary" link :icon="Delete" @click="deleteCategory(scope.row)"> 删除 </el-button>
      </template>
    </ProTable>
    <CustomerDialog ref="dialogRef" />
  </div>
</template>

<script setup lang="tsx" name="customer">
import { ref, reactive } from "vue";
import { Inventory } from "@/api/interface";
import ProTable from "@/components/ProTable/index.vue";
import { ProTableInstance, ColumnProps } from "@/components/ProTable/interface";
import { useHandleData } from "@/hooks/useHandleData";
import { generalStatus } from "@/utils/serviceDict";
import { getCustomList, createCustom, editCustom, deleteCustom } from "@/api/modules/inventory";
import CustomerDialog from "@/views/inventory/components/CustomerDialog.vue";
import { CirclePlus, EditPen, Delete } from "@element-plus/icons-vue";
import { getDicData } from "@/api/modules/system";

const proTable = ref<ProTableInstance>();
// 新建默认数据
const initData = reactive({
  id: 0,
  status: 1
});

const columns: ColumnProps<Inventory.ResCustomList>[] = [
  { type: "index", label: "序号", width: 80 },
  { sortable: true, prop: "customeCode", label: "编码", align: "left" },
  { sortable: true, prop: "name", label: "名称", align: "left" },
  { sortable: true, prop: "commTypeName", label: "通用类型", align: "left" },
  // {
  //   prop: "status",
  //   label: "状态",
  //   enum: generalStatus,
  //   minWidth: 120,
  //   align: "left",
  //   search: { el: "select", props: { filterable: true } }
  // },
  // { prop: "contact", label: "联系人",  align: "left" },
  // { prop: "mobile", label: "手机号", minWidth: 160, align: "left" },
  { prop: "operation", label: "操作", fixed: "right" }
];

const deleteCategory = async (params: Inventory.ResCustomList) => {
  await useHandleData(deleteCustom, { id: params.id }, `删除【${params.name}】通用类型`);
  proTable.value?.getTableList();
};

const dialogRef = ref<InstanceType<typeof CustomerDialog> | null>(null);
const openDialog = async (title: string, row: Partial<Inventory.ResCustomList> = {}) => {
  const { data } = await getDicData("CommonType");
  row.commTypeId = data[0].value;
  const params = {
    title,
    row: { ...row },
    typeList: data,
    isView: title === "查看",
    api: title === "新增" ? createCustom : title === "编辑" ? editCustom : undefined,
    getTableList: proTable.value?.getTableList
  };
  dialogRef.value?.acceptParams(params);
};
</script>
